<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php 
	$_categories = Mage::getResourceModel('catalog/category_collection');
    $_categories->addAttributeToSelect('url_key')
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('image')
            ->addAttributeToSelect('entity_id')
            ->addAttributeToSelect('all_children')
            ->addAttributeToSelect('is_anchor')
            ->addAttributeToFilter('is_active', 1)
			->addIdFilter($this->getCurrentCategory()->getChildren())
            ->setOrder('position', 'ASC')
            ->joinUrlRewrite()
            ->load();
?>
<?php $_collectionSize=is_array($_categories)?count($_categories):$_categories->count(); ?>
<?php $_columnCount = 5; ?>
<?php $i=0; foreach ($_categories as $_category): ?>
	<?php if ($i++%$_columnCount==0): ?>
	<ul class="products-grid">
	<?php endif ?>
	<?php if($_category->getIsActive()): ?>
	
		<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
			<div class="prodwrapper">
			<?php if($imageUrl = $_category->getImageUrl()):?>
			<a href="<?php echo $this->getCategoryUrl($_category) ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>" class="product-image"><img src="<?php echo $imageUrl ?>" width="154" height="220" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" /></a>
			<?php else: ?>
			<?php
			$productCollection = Mage::getResourceModel('catalog/product_collection');
			$productCollection->addAttributeToSelect('small_image,name,url_path');
    		$productCollection->addAttributeToFilter(array(array('attribute'=>'small_image','like'=>'/%')));
			//$productCollection->addAttributeToFilter('category_id',$_category->getId());
			$productCollection->setPageSize(1);
			$productCollection->setCurPage(1)->load();
			?>
			<?php foreach ($productCollection as $_product):?>
			<a href="<?php echo $this->getCategoryUrl($_category) ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image') ?>" width="154" height="220" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" /></a>
			<?php endforeach; ?>
			<?php endif;?>
			<h2 class="product-name"><a href="<?php echo $this->getCategoryUrl($_category) ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?></a></h2>
			<div class="image-top" style="*margin-top: -259px;"></div>
			<div class="image-bottom"></div>
	</div>
		</li>
	<?php endif; ?>
	<?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
	</ul>
	<?php endif ?>
<?php endforeach ?>